package cn.glenn.egaddress1.controller;

import cn.glenn.egaddress1.base.JsonResult;
import cn.glenn.egaddress1.mapper.AddressMapper;
import cn.glenn.egaddress1.pojo.dto.AddressAddParam;
import cn.glenn.egaddress1.pojo.enity.Address;
import cn.glenn.egaddress1.pojo.vo.AddressInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLOutput;
import java.util.List;

@RestController
@RequestMapping("/v1/address/")
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("add")
    public JsonResult addAddress(@RequestBody AddressAddParam param) {
        Address address = new Address();
        BeanUtils.copyProperties(param, address);
        int num = addressMapper.addAddress(address);
        System.out.println(num>0?"success":"fail");
        return new JsonResult(3000,"添加成功",null);
    }

    @GetMapping("list")
    public JsonResult showAddress() {
        List<AddressInfoVO> addressInfoVOS = addressMapper.getAddressInfo();
        System.out.println(addressInfoVOS);
        return new JsonResult(2000,"查询成功",addressInfoVOS);
    }

    @PostMapping("del")
    public JsonResult deleteAddress(Integer id) {
        int num = addressMapper.deleteAddress(id);
        return new JsonResult(4000,"删除成功",num);
    }
}
